home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmigaPlus / Tools / Development / AmigaTalk / general / KeyedCollection.st < prev    next >
Encoding:
Text File  |  2004-01-31  |  2.5 KB  |  116 lines

  1. Class KeyedCollection :Collection
  2. [
  3.    add: anElement
  4.  
  5.       ^ self error: 'Must add with explicit key'
  6. |
  7.    addAll: aCollection
  8.  
  9.       aCollection binaryDo: [:x :y | self at: x put: y].
  10.  
  11.       ^ aCollection
  12. |
  13.    asDictionary        ! newCollection !
  14.  
  15.       newCollection <- Dictionary new.
  16.  
  17.       self binaryDo: 
  18.          [:key :val ! newCollection at: key put: val].
  19.  
  20.       ^ newCollection
  21. |
  22.    at: key
  23.       
  24.       ^ self at: key ifAbsent:
  25.                  [self error:
  26.                      (key printString , ': association not found').
  27.                   
  28.                   ^ key
  29.                  ]
  30. |
  31.    atAll: aCollection put: anObject
  32.  
  33.       aCollection do: [:x ! self at: x put: anObject]
  34. |
  35.    binaryDo: aBlock  ! item !
  36.  
  37.       self do: [:x | aBlock value: self currentKey value: x ].
  38.  
  39.       ^ nil
  40. |
  41.    collect: aBlock 
  42.  
  43.       ^ self coerce:
  44.            (self inject: Dictionary new
  45.                    into: [:x :y ! x at: self currentKey
  46.                                    put: (aBlock value: y) . x ] )
  47. |
  48.    includesKey: key
  49.  
  50.       self at: key ifAbsent: [^ false].
  51.  
  52.       ^ true
  53. |
  54.    indexOf: anElement
  55.  
  56.       ^ self indexOf: anElement
  57.             ifAbsent: [self error: 'indexOf element not found']
  58. |
  59.    indexOf: anElement ifAbsent: exceptionBlock
  60.  
  61.       self do: [:x | (x = anElement) 
  62.                 ifTrue: [ ^ self currentKey ]].
  63.  
  64.       ^ exceptionBlock value
  65. |
  66.    keys ! newset !
  67.  
  68.       newset <- Set new.
  69.  
  70.       self keysDo: [:x | newset add: x].
  71.  
  72.       ^ newset
  73. |
  74.    keysDo: aBlock
  75.  
  76.       ^ self do: [ :x | aBlock value: self currentKey ]
  77. |
  78.    keysSelect: aBlock          
  79.  
  80.       ^ self coerce:
  81.            (self inject: Dictionary new
  82.                    into: [:x :y | (aBlock value: y currentKey)
  83.                                   ifTrue: [x at: self currentKey
  84.                                             put: y]. x ] )
  85. |
  86.    remove: anElement
  87.  
  88.       ^ self error: 'object must be removed with explicit key'
  89. |
  90.    removeKey: key
  91.  
  92.       ^ self removeKey: key ifAbsent:
  93.              [self error: 'no element associated with key'. ^ key]
  94. |
  95.    removeKey: key ifAbsent: exceptionBlock
  96.  
  97.       ^ self error: 'subclass should implement RemoveKey:ifAbsent:'
  98. |
  99.    select: aBlock          
  100.  
  101.       ^ self coerce:
  102.            (self inject: Dictionary 
  103.                    into: [:x :y | (aBlock value: y)
  104.                                    ifTrue: [x at: self currentKey put: y]. 
  105.                                   x 
  106.                          ] )
  107. |
  108.    values ! newbag !
  109.  
  110.       newbag <- Bag new.
  111.  
  112.       self do: [:x | newbag add: x].
  113.  
  114.       ^ newbag
  115. ]
  116.